﻿using Gfw.Models.Account;
using Gfw.Repository;
using NHibernate.Criterion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Gfw.Repositories.Account
{
    public class ActCompanyPayRepository : Repository<ActCompanyPay>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="payTypes"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalSize"></param>
        /// <returns></returns>
        public virtual IList<ActCompanyPay> Query(int[] payTypes, DateTime? start, DateTime? end, int pageIndex, int pageSize, out int totalSize)
        {
            var query = Session.CreateCriteria("ActCompanyPay");

            if (start.HasValue)
                query = query.Add(Expression.Ge("PayDate", start.Value));
            if (end.HasValue)
            {
                end = end.Value.AddDays(1);
                query = query.Add(Expression.Le("PayDate", end.Value));
            }

            if (payTypes != null) // == null 时查询全部
                query = query.Add(Expression.In("PayType", payTypes));

            query = query.AddOrder(new Order("PayDate", false));

            totalSize = (query.List().Count + pageSize - 1) / pageSize;
            return query.SetFirstResult(pageIndex * pageSize).SetMaxResults(pageSize).List<ActCompanyPay>();
        }
    }
}
